﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Hr;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainTravelApplicationQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private HrTravelApplicationEntity mSelectedTravelApp;
        public HrTravelApplicationEntity SelectedTravelApp
        {
            get { return mSelectedTravelApp; }
        }
        #endregion

        #region Constructor
        public frmMaintainTravelApplicationQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                condition += " AND TA.PERSON_ID = '" + person.PersonId + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND TA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpTravelDate.EditValue!=null)
            {
                condition += " AND TA.END_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpTravelDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00')";
            }
            if (dtpTravelDateTo.EditValue != null)
            {
                condition += " AND TA.START_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpTravelDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59')";
            }
            if (string.IsNullOrEmpty(txtTravelCode.Text) == false)
            {
                condition += " AND TA.TRAVEL_CODE LIKE '%" + txtTravelCode.Text + "%'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "TRAVEL_CODE DESC";
            DataTable dt = HrTravelApplicationBs.GetByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            cmbStatus.SetSelectedValue("STATUS03");
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedTravelApp = new HrTravelApplicationEntity(dr);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedTravelApp == null)
            {
                MessageBox.Show("请先选择一个出差单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult = DialogResult.OK;
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedTravelApp == null)
            {
                MessageBox.Show("请先选择一个出差单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult = DialogResult.OK;
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }
        #endregion

    }
}
